﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace kindly
{
    public partial class Title : LoggingPage
    {
        readonly string FontFamily = "Verdana";
        protected void Page_Load(object sender, EventArgs e)
        {
            string title = Request.QueryString["title"];

            if (string.IsNullOrEmpty(title))
            {
                title = "kindly.cz"; // fallback
            }

            string templateFile = Server.MapPath("~/" + "titleTemplate.png");

            string date = DateTime.Now.ToString("dddd, d.M.yyyy v HH:mm");

            // get title template and write periodical/date info
            using (Bitmap bm = new Bitmap(templateFile))
            {
                using (Graphics g = Graphics.FromImage(bm))
                {
                    float left, top;
                    Font titleFont = new Font(FontFamily, 30, FontStyle.Bold);
                    Font dateFont = new Font(FontFamily, 15);

                    SizeF titleSize = g.MeasureString(title, titleFont);
                    SizeF dateSize = g.MeasureString(date, dateFont);

                    // center text
                    Size templateSize = bm.Size;

                    // left aligned text, fixed left margin
                    left = 27f;
                    top = 600f;

                    // title
                    //left = (float)templateSize.Width / 2 - titleSize.Width / 2;                    
                    g.DrawString(title, titleFont, Brushes.Black, left, top);

                    // date
                    top += titleSize.Height + 10;
                    //left = (float)templateSize.Width / 2 - dateSize.Width / 2;
                    g.DrawString(date, dateFont, Brushes.Black, left, top);
                }

                // write image to response
                MemoryStream imageStream = new MemoryStream();
                bm.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imageContent = new Byte[imageStream.Length];
                // rewind the memory stream
                imageStream.Position = 0;
                // load the byte array with the image
                imageStream.Read(imageContent, 0, (int)imageStream.Length);

                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "image/jpeg";
                Response.AddHeader("Content-Length", imageContent.Length.ToString());
                Response.BinaryWrite(imageContent);
            }


        }

        private void fileNotFound()
        {
            Response.Clear();
            Response.StatusCode = 404;
            Response.StatusDescription = "File not found";
            Response.Flush();
            Response.Close();
        }
    }
}